package com.palm.test;

import com.palm.core.MenuManager;
import com.palm.core.util.SqlScriptRunner;
import com.palm.platform.PlatformModule;
import com.palm.platform.domain.User;
import org.noear.solon.Solon;
import org.noear.solon.annotation.Import;
import org.noear.solon.core.Aop;
import org.noear.solon.extend.sqltoy.DbManager;

import org.sagacity.sqltoy.dao.SqlToyDaoImpl;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.utils.DataSourceUtils;

import javax.sql.DataSource;
import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;


//@Configuration
@Import(PlatformModule.class)
public class SqlToyTestApp {
    //    @Bean
//    public DataSource db1(@Inject("${datasource}") BeeDataSource ds) {
//        return ds;
//    }
    public static void main1(String[] args) {
        String a=get("xxxx");
        System.out.println(a);
    }
    public static <T> T get(T t){
        StackTraceElement[] sms= Thread.currentThread().getStackTrace();
        return t;
    }
    public static void main(String[] args) throws FileNotFoundException {
        //String initSql=StringUtil.read();
        // SqlToyTestApp.class.getResourceAsStream()
        MenuManager.regMenu(TestMenu.class);
        DataSourceUtils.DBNameTypeMap.put("H22", DataSourceUtils.DBType.MYSQL);
        Solon.start(SqlToyTestApp.class, args, app -> {
            Aop.getAsyn(DataSource.class, bw -> {
                DataSource ds = bw.get();
                try (Connection conn = ds.getConnection()) {
                    InputStreamReader reader = new InputStreamReader(SqlToyTestApp.class.getResourceAsStream("/init.h2.sql"));
                    SqlScriptRunner runner = new SqlScriptRunner(conn);
                    runner.setLogWriter(null);
                    runner.runScript(reader);
                    reader.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        });
//        try (InputStream in = new FileInputStream("./test.xlsx")) {
//            List<TestVo> vos = TransportUtil.importExcel(in, TestVo.class);
//            System.out.println(vos);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        DataSourceUtils.DBNameTypeMap.put("H22", DataSourceUtils.DBType.MYSQL);
       // String sql = "select * from t_user";
//        SqlToyLazyDao dao1= DbManager.getDao(Aop.get(DataSource.class));
//        SqlToyDaoImpl dao = new SqlToyDaoImpl();//DbManager.getDao(Aop.get(DataSource.class));
//        dao.setDataSource(dao1.getDataSource());
//        dao.setSqlToyContext(dao1.getSqlToyContext());
//        User old=new User();
//        old.setId(1L);
//        old=dao.load(old);
//        System.out.println(old);
//        User newU=new User();
//        newU.setUsername("test update");
//       // DaoUtil.updateBy(dao,old,newU);
//        dao.update(old,newU);
//        old=dao.load(old);
//        dao.update(old);
//        System.out.println(old);
//  old=dao.load(old);
//        TestVo te = dao.loadBySql(sql, new TestVo());
//        System.out.println(te.getSName());
//        try (OutputStream out = new FileOutputStream("./test.xlsx")) {
//            TransportUtil.exportExcel(dao, sql, new TestVo(), out);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

    }
}
